{% extends 'layouts/master-page.html' %}
{% block title %}Most recent entries{% endblock %}
{% block head %}
<!-- css -->
<link href="/static/css/custom/filter.css" rel="stylesheet" />
<link href="/static/css/dataTables.bootstrap4.min.css" rel="stylesheet" />

<!-- javascript -->
<script type="text/javascript" src="/static/js/custom/filter.js"></script>
<script type="text/javascript" src="/static/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="/static/js/dataTables.bootstrap4.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
      setFilters();
      {% if errors %}
        setStatus("An error occured while parsing the filters. Please verify the input fields, like the dates", "danger");
      {% endif %}
    })
    function setFilters(){
      {% if filters is defined %}
        {% for key, val in filters.items() %}
          if($("#{{key}}").is(":checkbox")){
            if("{{val}}" === "on"){$("#{{key}}").prop('checked', true);}
          }else{
            $("#{{key}}").val("{{val}}");
          }
        {% endfor %}
        cvssSelectDisable()
        timeSelectDisable()
      {%endif%}
    }
  </script>
{% endblock %}
{% block content %}
<!-- breadcrumb -->
{% include 'subpages/breadcrumbs.html' %}
<!-- Filter options -->
{% include 'subpages/filters.html' %}
<!-- Search results -->
{% include 'subpages/table.html' %}
{% endblock %}

{% block body_scripts %}
<script>
function getFormData(top){

    if (top !== "top") {
      $('#filter-toggle').click();
    }

    var form_elements = document.getElementById('filter').elements;

    value_dict = {};

    for (var i = 0; i < form_elements.length; i++) {
        if (form_elements[i].id != "filter_send" && form_elements[i].id != "filter_reset") {
          value_dict[form_elements[i].id] = form_elements[i].value
        }
    }

    $.ajax({
            url: "/set_filter",
            type: "post",
            data: JSON.stringify(value_dict),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(response) {
               //
            },
            error: function(xhr) {
               //Do Something to handle error
            },
            complete: function(data) {
              var table = $('#CVEs').DataTable();
              table.ajax.reload();
              $('#filter_off').addClass('d-none');
              $('#filter_on').removeClass('d-none');
            }
        });
}

function reset_filters() {
  $.ajax({
            url: "/reset_filter",
            type: "get",
            success: function(response) {
               //
            },
            error: function(xhr) {
               //Do Something to handle error
            },
            complete: function(data) {
              var table = $('#CVEs').DataTable();
              table.ajax.reload();
              $('#filter_off').removeClass('d-none');
              $('#filter_on').addClass('d-none');
            }
        });
}
</script>
<script>
function ConvertDateTime(datetimestring) {

  var date = new Date(datetimestring)

  return date.toISOString().substring(0, 10) + ' - ' + date.toTimeString().substring(0, 5);
}
</script>
<script>
  function escapeHtml(unsafe) {
    return unsafe
         .replace(/&/g, "&amp;")
         .replace(/</g, "&lt;")
         .replace(/>/g, "&gt;")
         .replace(/"/g, "&quot;")
         .replace(/'/g, "&#039;");
 }
</script>
<script>
  $(document).ready(function() {
       $('#CVEs').DataTable({
           "processing": true,
           "serverSide": true,
           "ajax": {
               "url": "/fetch_cve_data",
               "data": function ( d ) {
                   d.retrieve = "cves";
               },
               "type": "POST"
           },
           "columnDefs": [
             {"searchable": false, "targets": 0 },
             {bSortable: false, targets: [0, 4]},
           ],
           "order": [[ 5, "desc" ]],
           "orderMulti": false,
           "columns": [
               { "data": "blank",
                   render : function(data, type, row) {
                  return ''
               }
               },
               { "data": "id" ,
                   render : function(data, type, row) {
                      return '<a href="/cve/'+ row['id'] + '" width="20" height="20" title="Active">'+ row['id'] +'</a>'
                   }
               },
               { "data": "cvss"},
               { "data": "cvss3"},
               { "data": "summary" ,
                   render : function(data, type, row) {
                      return escapeHtml(row['summary'])
                   }
               },
               { "data": "last-modified" ,
                   render : function(data, type, row) {
                 var dtg = ConvertDateTime(row['last-modified'])
                 return dtg
               }
               },
               { "data": "Published",
                   render : function(data, type, row) {
                 var dtg = ConvertDateTime(row['Published'])
                 return dtg
               }
               },
           ],
           "iDisplayLength": {{pageLength}},
           "language": { "processing": "<img src='{{ url_for('static', filename='img/ajaxload.gif') }}'> Loading...",
               "zeroRecords": "No records to display", searchPlaceholder: "Regex search...", search: ""},
           "search": {
               "regex": true
           }
       });
       $('#CVEs').removeClass('d-none');

       $.ajax({
            url: "/get_filter",
            type: "get",
            success: function(response) {
               //
            },
            error: function(xhr) {
               //
            },
            complete: function(data) {

                var isTrueSet = (data.responseJSON === true);

                if (isTrueSet) {
                    $('#filter_off').removeClass('d-none');
                    $('#filter_on').addClass('d-none');
                }
                else {
                    $('#filter_off').addClass('d-none');
                    $('#filter_on').removeClass('d-none');
                }

            }
        });

  } );
</script>
{% endblock %}